import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
  path: '/',
  name: 'home',
  component: () => import('../views/Home.vue'),
  redirect: "/homepage",
  children: [{
    path: '/homepage',
    name: 'homepage',
    component: () => import('../views/oneRouter/homePage.vue'),
    children: [{
      path: '/post',
      name: 'post',
      component: () => import('../views/twoRouter/post.vue')
    },
     
    {
      path: '/productlist',
      name: 'productlist',
      component: () => import('../views/twoRouter/productList.vue')
    },
    {
      path: '/photo',
      name: 'photo',
      component: () => import('../views/twoRouter/photo.vue')
    },
    ]
  },
  {
    path: '/commodity',
    name: 'commodity',
    component: () => import('../views/oneRouter/commodity.vue'),
    children: [
      {
        path: '/productlists',
        name: 'productLists',
        component: () => import('../views/twoRouter/productLists.vue')
      },
      {
        path: '/categoryList',
        name: 'categoryList',
        component: () => import('../views/twoRouter/categoryList.vue')
      },
    ]
  },
  {
    path: '/orderfrom',
    name: 'orderfrom',
    component: () => import('../views/oneRouter/orderForm.vue'),
  },
  {
    path: '/member',
    name: 'member',
    component: () => import('../views/oneRouter/member.vue'),

  },
  {
    path: '/set',
    name: 'set',
    component: () => import('../views/oneRouter/set.vue')
  },
  ]
},
{
  path: '/login',
  name: 'login',
  component: () => import('../views/Login.vue')
},
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  var loginstatus = localStorage.getItem("token")
  if (to.name != "login") {
    if (loginstatus) {
      next()
    } else {
      next("/login")
    }
  } else {
    next()
  }
})

export default router